package util;

import model.Student;
import model.StudentScore;
import model.Topic;
import model.User;

import java.io.*;
import java.util.ArrayList;

public class Io {

    //todo 合成复用原则
    private static ArrayList<Student> ls;
    public Io(ArrayList<Student> ls){
        this.ls = ls;
    }

    public Io() {

    }


    /**
     * 读出学生信息到系统
     */
    public static ArrayList<Student> read(){
        final File file = new File("d:/a.txt");
        ObjectInputStream ois = null;
        if(file.length()!=0){
            try {
                System.out.println("正在从文件中读取。。。");
                Thread.sleep(1000);
                ois = new ObjectInputStream(new FileInputStream("d:/a.txt"));
                // 写入数组中
                return (ArrayList<Student>)ois.readObject();
            } catch (IOException | ClassNotFoundException | InterruptedException e) {
                e.printStackTrace();
            }finally {
                if(null!=ois){
                    try {
                        ois.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
        return new ArrayList<>();
    }
    /**
     * 写入学生信息到文件
     */
    public static void write(ArrayList<Student> ls){
        ObjectOutputStream fw = null;

        try {
            fw = new ObjectOutputStream(new FileOutputStream("d:/a.txt"));
            fw.writeObject(ls);
            fw.reset();
            System.out.println("写入数据成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!= fw){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /*public static void main(String[] args) {
        ArrayList<Student> ls = new ArrayList();
        ls.add(new Student(1,"oyz",24,new User("oyz","123")));
        ls.add(new Student(2,"mz",34,new User("mz","123")));
        ls.add(new Student(3,"xyg",24,new User("oyz","123")));
        Io io = new Io();
        io.write(ls);
    }*/

    /**
     * 写考题到文件中
     */
    public static void saveTopic(ArrayList<Topic> arrayList) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/Topic.txt"));
        oos.writeObject(arrayList);
        oos.reset();
    }

   /* public static void main(String[] args) {
        ArrayList<Topic> arrayList = new ArrayList<>();
        arrayList.add(new Topic(1,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(2,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(3,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(4,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(5,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(6,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(7,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(8,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(9,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(10,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(11,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(12,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        arrayList.add(new Topic(13,"世界上谁最好看？","a.马哲","b.欧阳钊","c.熊一谷","d.李经理","b.欧阳钊"));
        Io io = new Io();
        try {
            io.saveTopic(arrayList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/

    /**
     * 读取文件的考题到系统
     */
    public static ArrayList<Topic> readTopic() throws IOException, ClassNotFoundException {
        File file = new File("d:/Topic.txt");
        if(file.length()!=0){
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:/Topic.txt"));
            System.out.println("读取考题成功!");
            return (ArrayList<Topic>)ois.readObject();
        }
        return new ArrayList<>();
    }
    /**
     * 导出分数
     */
    public static void saveSore(ArrayList<StudentScore> arrayList) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/StudentScore.txt"));
        oos.writeObject(arrayList);
        oos.reset();
    }
    /**
     * 导入分数，在学员登录后
     */
    public static ArrayList<StudentScore> readScore() throws IOException, ClassNotFoundException {
        File file = new File("d:/StudentScore.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        // 文件不等于0才开始读文件
        if(file.length()!=0){
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:/StudentScore.txt"));
             return (ArrayList<StudentScore>)ois.readObject();
        }
        return new ArrayList<>();
    }
}
